﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreBDS
{
    public class FieldOfBusinessBll
    {
        public static int Insert(Cvk_FieldOfBusiness info, string strConnect)
        {
            int result = 0;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    db.Cvk_FieldOfBusinesses.InsertOnSubmit(info);
                    db.SubmitChanges();
                    result = info.FieldId;
                }
                catch { }
                finally { db.Dispose(); }
            }
            return result;
        }
        public static bool Delete(int id, string strConnect)
        {
            bool result = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    Cvk_FieldOfBusiness info = db.Cvk_FieldOfBusinesses.Single(p => p.FieldId == id);
                    if (info != null)
                    {
                        db.Cvk_FieldOfBusinesses.DeleteOnSubmit(info);
                        db.SubmitChanges();
                        result = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return result;
        }
        public static void Update(Cvk_FieldOfBusiness info, string strConnect)
        {
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    Cvk_FieldOfBusiness infoOld = db.Cvk_FieldOfBusinesses.Single(p => p.FieldId == info.FieldId);
                    if (infoOld != null)
                    {
                        infoOld.Name = info.Name;
                        db.SubmitChanges();
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
        }
        public static bool UpdateDeleted(int id, bool isDeleted, string strConnect)
        {
            bool result = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    Cvk_FieldOfBusiness infoOld = db.Cvk_FieldOfBusinesses.Single(p => p.FieldId == id);
                    if (infoOld != null)
                    {
                        infoOld.IsDeleted = isDeleted;
                        db.SubmitChanges();
                        result = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return result;
        }
        public static bool UpdateActived(int id, bool isActive, string strConnect)
        {
            bool result = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    Cvk_FieldOfBusiness infoOld = db.Cvk_FieldOfBusinesses.Single(p => p.FieldId == id);
                    if (infoOld != null)
                    {
                        infoOld.IsActive = isActive;
                        db.SubmitChanges();
                        result = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return result;
        }

        public static Cvk_FieldOfBusiness GetInfo(int FieldId, string strConnect)
        {
            Cvk_FieldOfBusiness info = new Cvk_FieldOfBusiness();
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    info = db.Cvk_FieldOfBusinesses.Single(p => p.FieldId == FieldId);
                }
                catch { info = null; }
                finally { db.Dispose(); }
            }
            return info;
        }
        public static string GetName(int FieldId, string strConnect)
        {
            string result = string.Empty;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    result = db.Cvk_FieldOfBusinesses.Single(p => p.FieldId == FieldId).Name;
                }
                catch { result = string.Empty; }
                finally { db.Dispose(); }
            }
            return result;
        }
        public static List<Cvk_FieldOfBusiness> GetList(string strConnect)
        {
            List<Cvk_FieldOfBusiness> list = new List<Cvk_FieldOfBusiness>();
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    var varList = from p in db.Cvk_FieldOfBusinesses
                                  where p.IsDeleted == false
                                  select p;
                    list = varList.ToList();
                }
                catch { list = null; }
                finally { db.Dispose(); }
            }
            return list;
        }
    }
}
